using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using Controls;

namespace PCR.PCRControls
{
	public partial class TimeSpan : UserControl, INotifyPropertyChanged, IComponentConnector
	{
		private string _FocusButton = "second";

		public static readonly DependencyProperty MinuteProperty = DependencyProperty.Register("Minute", typeof(int), typeof(TimeSpan), new FrameworkPropertyMetadata(0, OnMinuteChanged));

		public static readonly DependencyProperty SecondProperty = DependencyProperty.Register("Second", typeof(int), typeof(TimeSpan), new FrameworkPropertyMetadata(0, OnSecondChanged));

		public int Minute
		{
			get
			{
				return (int)GetValue(MinuteProperty);
			}
			set
			{
				if (value < 0)
				{
					value = 0;
				}
				if (value > 99)
				{
					value = 99;
				}
				if (value != (int)GetValue(MinuteProperty))
				{
					SetValue(MinuteProperty, value);
					SMinute = value.ToString().PadLeft(2, '0');
				}
			}
		}

		public string SMinute
		{
			get
			{
				return Minute.ToString().PadLeft(2, '0');
			}
			set
			{
				int result = 0;
				int.TryParse(value, out result);
				if (result < 0)
				{
					result = 0;
				}
				if (result > 99)
				{
					result = 99;
				}
				if (Minute != result)
				{
					Minute = result;
				}
				OnPropertyChanged("SMinute");
			}
		}

		public int Second
		{
			get
			{
				return (int)GetValue(SecondProperty);
			}
			set
			{
				if (value < 0)
				{
					value = 0;
				}
				if (value > 59)
				{
					value = 59;
				}
				if (value != (int)GetValue(SecondProperty))
				{
					SetValue(SecondProperty, value);
					SSecond = value.ToString().PadLeft(2, '0');
				}
			}
		}

		public string SSecond
		{
			get
			{
				return Second.ToString().PadLeft(2, '0');
			}
			set
			{
				int result = 0;
				int.TryParse(value, out result);
				if (result < 0)
				{
					result = 0;
				}
				if (result > 59)
				{
					result = 59;
				}
				if (Second != result)
				{
					Second = result;
				}
				OnPropertyChanged("SSecond");
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		public TimeSpan()
		{
			InitializeComponent();
			TextBoxEx textBoxEx = second;
			RoutedEventHandler value = delegate
			{
				second.Focus();
			};
			textBoxEx.Loaded += value;
		}

		public void OnPropertyChanged(string propName)
		{
			if (string.IsNullOrEmpty(propName))
			{
				throw new ArgumentException("propName");
			}
			if (this.PropertyChanged != null)
			{
				this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
			}
		}

		private static void OnMinuteChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			TimeSpan timeSpan = obj as TimeSpan;
			timeSpan.minute.Text = timeSpan.Minute.ToString().PadLeft(2, '0');
		}

		private static void OnSecondChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
		{
			TimeSpan timeSpan = obj as TimeSpan;
			timeSpan.second.Text = timeSpan.Second.ToString().PadLeft(2, '0');
		}

		private void TextBox_GotFocus(object sender, RoutedEventArgs e)
		{
			_FocusButton = (sender as TextBox).Name;
		}

		private void Up_Click(object sender, RoutedEventArgs e)
		{
			if (_FocusButton == "second")
			{
				Second++;
			}
			else
			{
				Minute++;
			}
		}

		private void Down_Click(object sender, RoutedEventArgs e)
		{
			if (_FocusButton == "second")
			{
				Second--;
			}
			else
			{
				Minute--;
			}
		}

		public new bool Focus()
		{
			bool result;
			if (_FocusButton == "second")
			{
				result = second.Focus();
				minute.Focusable = true;
			}
			else
			{
				result = minute.Focus();
			}
			return result;
		}

		private void minute_GotFocus(object sender, RoutedEventArgs e)
		{
			_FocusButton = (sender as TextBox).Name;
		}

		private void second_GotFocus(object sender, RoutedEventArgs e)
		{
			_FocusButton = (sender as TextBox).Name;
		}
	}
}
